package com.css.fxfzypg.modules.baseData.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.repository.RoadRepository;
import com.css.fxfzypg.modules.baseData.entity.Road;
import com.css.fxfzypg.modules.baseData.service.RoadService;
import com.css.fxfzypg.modules.baseData.queryParams.RoadQueryParams;
import com.css.fxfzypg.modules.baseData.vo.RoadVO;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@Service
public class RoadServiceImpl implements RoadService {

    @Autowired
    RoadRepository roadRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RoadVO updateRoad(RoadVO roadVO) {
        Road road = roadVO.getRoad();
        Road road2 = roadRepository.updateRoadById(road);

        return new RoadVO(road2);
    }

    @Override
    public RoadVO getRoadById(Integer id) {
        Road road = roadRepository.getRoadById(id);
        return new RoadVO(road);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoad(List<Integer> ids) {

        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        roadRepository.deleteRoadsById(ids,timestamp);
    }

    @Override
    @Transactional
    public Road saveRoad(Road road) {
        road.setRoadStatus(YNEnum.Y.toString());
        return roadRepository.saveRoad(road);
    }

    @Override
    public JSONObject getAllRoad(RoadQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(roadRepository.getAllRoadParams(queryParams));
    }

    @Override
    public List<Road> getAll() {

        return roadRepository.getAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<Road> roads = FileUtil.importExcel(file, Road.class);

        for(int i = 0 ; i<roads.size();i++){

            roads.get(i).setRoadStatus(YNEnum.Y.toString());

        }
        roadRepository.importExcel(roads);
    }
}
